<template>
  <a-card v-if="GroupStore">
    <template #title>
      <span>{{ GroupStore.groupnick }} 的配置</span>
    </template>
    <!-- 示例配置项 -->
    <!-- {{ GroupStore }} -->
    <a-form>
      <a-form-item field="issave" label="自动保存">
        <a-switch v-model="GroupStore.issave" @change="(value) => handleSaveChange(Boolean(value))"/>
      </a-form-item>
      <a-form-item field="isauto" label="自动处理">
        <a-switch v-model="GroupStore.isauto" @change="(value) => handleAutoChange(Boolean(value))"/>
      </a-form-item>
    </a-form>
  </a-card>
</template>

<script lang="ts" setup>
  import { watch } from 'vue';  
  import { useGroupStore } from '@/store';
  import { Message } from '@arco-design/web-vue'
   import { editGroup } from '@/api/weixin';

  const GroupStore = useGroupStore();

  const props = defineProps<{
    selectedWxid: string;
  }>();
  // 处理保存群消息开关
  const handleSaveChange = async (value: boolean) => {
    try {
      const res: any = await editGroup({
        mywxid: props.selectedWxid  ,
        wxid: GroupStore.groupwxid,
        field: {
          issave: value ? 1 : 0,
        },
      });
      if (res.code === 20000) {
        Message.success('设置成功');
        GroupStore.selectedGroup(res.data.wxid,res.data.avatarMaxUrl,res.data.nick,value,res.data.isauto)
      } else {
        Message.error(res.msg || '设置失败');
      }
    } catch (error) {
      Message.error('设置失败');
    }
  };
  const handleAutoChange = async (value: boolean) => {
    try {
      const res: any = await editGroup({
        mywxid: props.selectedWxid  ,
        wxid: GroupStore.groupwxid,
        field: {
          isauto: value ? 1 : 0,
        },
      });
      if (res.code === 20000) {
        Message.success('设置成功');
        GroupStore.selectedGroup(res.data.wxid,res.data.avatarMaxUrl,res.data.nick,res.data.issave,value)
      } else {
        Message.error(res.msg || '设置失败');
      }
    } catch (error) {
      Message.error('设置失败');
    }
  };
  // 监听群变化
  watch(
    () => GroupStore.groupwxid,
    (newWxid) => {
      if (newWxid) {
        console.log('监听到群变化', GroupStore);
      }
    },
    { immediate: true }
  );
</script>
